﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ExBug
{
    public partial class OrdonantariSearchFrm : FrmRezumat
    {

        public String Filtru = String.Empty;
        public Boolean bCauta = true;
        public Boolean parteneriProprii = false;
        public Boolean perioadaCurenta = false;
        //private int lunaCurenta = 0;
        //private int anulCurent = 0;

        public OrdonantariSearchFrm(ModuleClass mc,Boolean _partprop,Boolean _perCrt) :base(mc)
        {
            InitializeComponent();
            workConnString = mc.workConnectionString;
            ActiveControl = numarulTB;
            parteneriProprii = _partprop;
            EnableMainBrowseButton = false;
            perioadaCurenta = _perCrt;
            //lunaCurenta = _lunaCurenta;
           // anulCurent = _anulCurent;
        }

        private void DocumenteSearchFrm_Load(object sender, EventArgs e)
        {
            SqlConnection con = DeschideConexiune(workConnString);
            this.parteneriTableAdapter.Connection = con;
            if(!parteneriProprii)
                this.parteneriTableAdapter.Fill(this.eSRPDataSet.Parteneri);
            else
                this.parteneriTableAdapter.FillByUnitate(this.eSRPDataSet.Parteneri,ModuleInfo.CodUnitate);
            InchideConexiune(con);
            AddButton("Filtreaza[F9]", global::ERPStudio.Properties.Resources._001_371, Cauta, ToolStripItemAlignment.Right, false);
            //AddButton("Filtreaza", global::ERPStudio.Properties.Resources._001_38, Filtreaza, ToolStripItemAlignment.Right, false);
            HandleSalvare += Cauta;
        }

        private void addPartenerB_Click(object sender, EventArgs e)
        {
            furnizoriArticoleSelectateBindingSource.Add(new FurnizoriArticoleSelectate(partenerCB.Text,
                Convert.ToInt16(partenerCB.SelectedValue)));
            parteneriRB.Checked = true;
        }

        private void delPart_Click(object sender, EventArgs e)
        {
            furnizoriArticoleSelectateBindingSource.RemoveCurrent();
        }

        private void deLaDataDTP_Enter(object sender, EventArgs e)
        {
            dateRB.Checked = true;
        }

        private void laDataDTP_Enter(object sender, EventArgs e)
        {
            dataRB.Checked = true;
        }

        private void partenerCB_Enter(object sender, EventArgs e)
        {
            partenerRB.Checked = true;
        }

        new private void Cauta(object sender, EventArgs e)
        {
            Filtru = String.Empty;
            if(parteneriProprii)
                Filtru = " CodUnitate=" + ModuleInfo.CodUnitate.ToString();
          
            /*if (perioadaCurenta)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru += " MONTH([DataDoc]) >= " + lunaCurenta.ToString() + " AND YEAR([DataDoc])>=" + anulCurent.ToString();              
            }
            */
            if (dataRB.Checked)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru = "DataDoc='" + laDataDTP.Value.Year.ToString() + "-" + laDataDTP.Value.Month.ToString() + "-" +
                    laDataDTP.Value.Day.ToString() + "'";
            }
            if (dateRB.Checked)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";

                Filtru = "DataDoc>='" + deLaDataDTP.Value.Year.ToString() + "-" + deLaDataDTP.Value.Month.ToString() + "-" +
                    deLaDataDTP.Value.Day.ToString() + "' and DataDoc<='" + panaLaDataDTP.Value.Year.ToString() + "-" + panaLaDataDTP.Value.Month.ToString() + "-" +
                    panaLaDataDTP.Value.Day.ToString() + "'";
            }
            if (partenerRB.Checked && partenerCB.SelectedValue!=null)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru += " IDFurnizor=" + partenerCB.SelectedValue.ToString();
            }
            if (parteneriRB.Checked)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru += " IDFurnizor in (";
                String filt = String.Empty;
                for (Int16 i = 0; i < furnizoriArticoleSelectateBindingSource.Count; i++)
                {
                    if (filt != String.Empty)
                        filt += ",";
                    filt += ((FurnizoriArticoleSelectate)furnizoriArticoleSelectateBindingSource.List[i]).Tag.ToString();
                }
                Filtru += filt + ")";
            }
            
            if (numarulTB.Text != String.Empty)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru += " NrDoc = " + numarulTB.Text ;
            }
            

            ordonantariTableAdapter.Connection = DeschideConexiune(workConnString);
            ordonantariTableAdapter.FillByFiltru(eSRPDataSet.Ordonantari, Filtru, ModuleInfo.AnArhiva);
            InchideConexiune(ordonantariTableAdapter.Connection);
            if (ordonantariBindingSource.Count == 0)
            {
                MessageBox.Show("Nu s-a gasit nici o inregistrare care sa satisfaca criteriile selectate!",
                       "Atentie",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Hand);
                return;
            }
            this.DialogResult = DialogResult.OK;
            Close();

        }

        private void Filtreaza(object sender, EventArgs e)
        {
            bCauta = false;
            Cauta(sender,e);
        }

        private void dateRB_CheckedChanged(object sender, EventArgs e)
        {
            if (dateRB.Checked)
                dataRB.Checked = false;
        }

        private void dataRB_CheckedChanged(object sender, EventArgs e)
        {
            if (dataRB.Checked)
                dateRB.Checked = false;
        }

        private void partenerRB_CheckedChanged(object sender, EventArgs e)
        {
            if (partenerRB.Checked)
                parteneriRB.Checked = false;

        }

        private void parteneriRB_CheckedChanged(object sender, EventArgs e)
        {
            if (parteneriRB.Checked)
                partenerRB.Checked = false;

        }
    }

    [Serializable]
    public class FurnizoriArticoleSelectate
    {
        public FurnizoriArticoleSelectate(String nume, Int16 tag)
        {
            _Name = nume;
            _Tag = tag;
        }

        private String _Name = String.Empty;
        [System.ComponentModel.Bindable(true)]
        public String Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
        private Int16 _Tag = 0;
        [System.ComponentModel.Bindable(true)]
        public Int16 Tag
        {
            get
            {
                return _Tag;
            }
            set
            {
                _Tag = value;
            }
        }
    }
}
